package com.kingdee.eas.custom.common.server;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.hr.assessment.DbFacadeFactory;
import com.kingdee.eas.custom.hr.assessment.base.AssessmentTimeConfigCollection;
import com.kingdee.eas.custom.hr.assessment.base.AssessmentTimeConfigFactory;
import com.kingdee.eas.custom.hr.assessment.base.AssessmentTimeConfigInfo;
import com.kingdee.eas.custom.hr.attendance.AttendanceTimeConfigInfo;
import com.kingdee.eas.hr.canon.cbase.GradeFactory;
import com.kingdee.eas.hr.canon.cbase.GradeInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;

public class CanonUtils {

	public static AttendanceTimeConfigInfo getAttendancePeriod(Context ctx,
			Date date) throws BOSException {
		AttendanceTimeConfigInfo period = null;

		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

		String str1 = f.format(date);
		String str2 = f.format(date);
		String sql = "/*dialect*/SELECT * FROM CT_KQ_ATTENDANCETIMECONFIG WHERE TO_CHAR(FBEGINDATE,'YYYY-MM-DD')<='"
				+ str1 + "' AND TO_CHAR(FENDDATE,'YYYY-MM-DD')>='" + str2 + "'";

		IRowSet rs = null;

		if (ctx == null) {
			rs = DbFacadeFactory.getRemoteInstance().getQuery(sql, null);
		} else {
			rs = DbUtil.executeQuery(ctx, sql);
		}

		try {
			while (rs.next()) {
				period = new AttendanceTimeConfigInfo();
				period.setId(BOSUuid.read(rs.getString("FID")));
				period.setNumber(rs.getString("FNUMBER"));
				period.setName(rs.getString("FNAME_L2"));
				period.setBeginDate(rs.getDate("FBEGINDATE"));
				period.setEndDate(rs.getDate("FENDDATE"));
			}
		} catch (SQLException e) {
			throw new BOSException(e);
		}

		return period;
	}

	public static AssessmentTimeConfigInfo getAssessmentPeriod(Context ctx,
			Date date) throws BOSException {
		AssessmentTimeConfigInfo period = null;

		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

		String str1 = f.format(date);
		String str2 = f.format(date);
		String sql = "/*dialect*/SELECT * FROM T_BAS_ASSESSMENTTIMECONFIG WHERE TO_CHAR(FBEGINDATE,'YYYY-MM-DD')<='"
				+ str1 + "' AND TO_CHAR(FENDDATE,'YYYY-MM-DD')>='" + str2 + "'";

		IRowSet rs = null;

		if (ctx == null) {
			rs = DbFacadeFactory.getRemoteInstance().getQuery(sql, null);
		} else {
			rs = DbUtil.executeQuery(ctx, sql);
		}

		try {
			while (rs.next()) {
				period = new AssessmentTimeConfigInfo();
				period.setId(BOSUuid.read(rs.getString("FID")));
				period.setNumber(rs.getString("FNUMBER"));
				period.setName(rs.getString("FNAME_L2"));
				period.setBeginDate(rs.getDate("FBEGINDATE"));
				period.setEndDate(rs.getDate("FENDDATE"));
			}
		} catch (SQLException e) {
			throw new BOSException(e);
		}

		return period;
	}

	public static String getPayGrade(Context ctx, BigDecimal salary,
			GradeInfo grade) throws EASBizException, BOSException {
		grade = GradeFactory.getLocalInstance(ctx).getGradeInfo(
				new ObjectUuidPK(grade.getId()));

		String payGrade = null;

		if (salary.compareTo(grade.getBaseEntry()) < 0) {
			payGrade = "U";
		} else if (salary.compareTo(grade.getBaseEntry()) >= 0
				&& salary.compareTo(grade.getBase2nd()) < 0) {
			payGrade = "A";
		} else if (salary.compareTo(grade.getBase2nd()) >= 0
				&& salary.compareTo(grade.getBaseMid()) < 0) {
			payGrade = "B";
		} else if (salary.compareTo(grade.getBaseMid()) >= 0
				&& salary.compareTo(grade.getBase3rd()) < 0) {
			payGrade = "C";
		} else if (salary.compareTo(grade.getBase3rd()) >= 0
				&& salary.compareTo(grade.getBaseMax()) < 0) {
			payGrade = "D";
		} else if (salary.compareTo(grade.getBaseMax()) >= 0) {
			payGrade = "E";
		}
		return payGrade;
	}

	public static AssessmentTimeConfigCollection getAssessmentTimeConfigCollection(
			Context ctx, String sNum, String eNum) throws BOSException {

		if (sNum == null || eNum == null || sNum.equals("") || eNum.equals("")) {
			return new AssessmentTimeConfigCollection();
		} else {
			return AssessmentTimeConfigFactory.getLocalInstance(ctx)
					.getAssessmentTimeConfigCollection(
							"where number >= '" + sNum + "' and number <='"
									+ eNum + "'");
		}

	}
}
